package org.leon.mybatis.test;

import org.apache.ibatis.session.SqlSession;
import org.leon.mybatis.domain.Clazz;
import org.leon.mybatis.domain.Person;
import org.leon.mybatis.domain.Student;
import org.leon.mybatis.domain.User;
import org.leon.mybatis.factory.MySqlSessionFactory;
import org.leon.mybatis.mapper.PersonMapper;
import org.leon.mybatis.mapper.UserAnnotationMapper;

import java.util.List;

import static java.lang.System.out;

/**
 * Created by HuGuodong on 2017/4/29.
 */
public class UserSqlSessionFactory {

    public static void insert(){
        SqlSession session = MySqlSessionFactory.getSqlSession();
        User user = new User("胡国栋", "M", 29);
        session.insert("org.leon.mybatis.mapper.UserMapper.save", user);
        MySqlSessionFactory.releaseSqlSession(session);
    }

    public static void select(){
        SqlSession session = MySqlSessionFactory.getSqlSession();
        User user = session.selectOne("org.leon.mybatis.mapper.UserMapper.selectUser", 2);
        out.println(user);
        MySqlSessionFactory.releaseSqlSession(session);
    }

    public static void update(){
        SqlSession session = MySqlSessionFactory.getSqlSession();
        User user = session.selectOne("org.leon.mybatis.mapper.UserMapper.selectUser", 2);
        out.println(user);
        user.setSex("男");
        session.update("org.leon.mybatis.mapper.UserMapper.modifyUser", user);
        user = session.selectOne("org.leon.mybatis.mapper.UserMapper.selectUser", 2);
        out.println(user);
        MySqlSessionFactory.releaseSqlSession(session);

    }

    public static void delete(){
        SqlSession session = MySqlSessionFactory.getSqlSession();
        session.delete("org.leon.mybatis.mapper.UserMapper.removeUser",1);
        MySqlSessionFactory.releaseSqlSession(session);
    }

    /**
     * use resultMap -> simple
     */
    public static void selectUser2(){
        SqlSession session = MySqlSessionFactory.getSqlSession();
        List<User> user_list = session.selectList("org.leon.mybatis.mapper.UserMapper.selectUser2");
        for (User u:user_list) {
            out.println(u);
        }
        MySqlSessionFactory.releaseSqlSession(session);
    }

    public static void selectStudent(){
        SqlSession session = MySqlSessionFactory.getSqlSession();
        List<Student> list_student = session.selectList("org.leon.mybatis.mapper.UserMapper.selectStudent");
        for (Student s:
                list_student) {
            out.println(s);
        }
        MySqlSessionFactory.releaseSqlSession(session);
    }

    public static void selectClazz(){
        SqlSession session = MySqlSessionFactory.getSqlSession();
        List<Clazz> clazz_list = session.selectList("org.leon.mybatis.mapper.UserMapper.selectClazz");
        clazz_list.forEach(s->out.println(s));
        MySqlSessionFactory.releaseSqlSession(session);
    }

    /**
     * OneToOneTest
     */
    public static void selectPersonById(){
        SqlSession session = MySqlSessionFactory.getSqlSession();
        PersonMapper pm = session.getMapper(PersonMapper.class);
        Person p = pm.selectPersonById(1);
        // 查询Person信息时Person对应的Card也查询出来了。
        out.println(p);
        MySqlSessionFactory.releaseSqlSession(session);
    }


}
